<template>
	<view class="container">

		<scroll-view :style="'height:'+windowHeight + 'rpx'" scroll-y="true" class="scroll-Y" v-if="dataList.length>0"
			:refresher-enabled='false' @scrolltolower="onreachBottomMore" @refresherabort="onAbort">
			<view style=" height: 100%;">


				<block v-for="(item, index) in dataList" :key="index" v-if="dataList.length>0">
					<view :data-id="item.id" :data-item="item" @click="itemCleck(item)">

						<view class="list-cell-up">
							<view class="layout-up">
								<view class="default-button" v-if="item.isDefault==true">默认</view>
								<view class="default-button" v-else style=" visibility:hidden">默认</view>
								<view class="list-content_text">
									<view class="layout-1">
										<view class="layout-personal-info">
											<view class="text-1" style="margin-right: 10px;">{{item.addressName}}</view>
											<view class="text-1">{{item.addressPhone}}</view>
										</view>
										<image class="edit-icon"
											src="@appstatic/img/my/edit_icon.png"
											@click="editAddress(item)">
									</view>
									<view class="text-2" style="margin-left: 10px;margin-right: 50px;">
										{{item.addressProvince}} {{item.addressCity}} {{item.addressArea}}
										{{item.addressDetails}}</view>
								</view>
							</view>
						</view>
						<view class="list-cell-down">

							<view class="layout-2">
								<view class="layout-personal-info" v-if="item.isDefault==true"
									@click="defaultAddress(item)">
									<image class="choose-icon"
										src="@appstatic/img/my/choose_icon.png">
										<view class="text-4">默认地址</view>
								</view>
								<view class="layout-personal-info" v-else @click.stop="defaultAddress(item)">
									<image class="choose-icon"
										src="@appstatic/img/my/unchoose_icon.png">
										<view class="text-5">默认地址</view>
								</view>
								<view class="text-3" @click.stop="showDeleteAddressDialog(item)">删除</view>
							</view>
						</view>

					</view>




				</block>
				<view class="noMore" v-if="pagination.loadMore == false">
					<text>没有更多数据了</text>
				</view>
			</view>

		</scroll-view>



		<view class="addBtn" @click="addAdress">+添加新地址</view>
		<clxDialog :isShow='show' @dialogConfirm='dialogConfirm' @dialogCancel='dialogCancel' :title="title">
		</clxDialog>

	</view>
</template>

<script>
	const app = getApp();
	const util = require('@utils/util.js');
	const network = require('@manager/network-manager.js');
	import clxDialog from "../../../components/clx-dialog/clx-dialog";
	export default {
		data() {
			return {
				windowHeight: 0,
				dataList: [],
				title: '是否删除地址？',
				show: false,
				planDeleteId: '',
				pagination: {
					page: 1,
					totalPage: 1,
					limit: 500,
					length: 100,
					loadMore: true,
				},
				from:''
			}
		},
		components: {
			clxDialog
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			var that = this;
			that.from = options.from
			uni.getSystemInfo({
				success: function(res) {
					console.log(res, 'getSystemInfo');
					let clientHeight = res.windowHeight - 44 - 42;

					let clientWidth = res.windowWidth;
					let ratio = 750 / clientWidth; //计算为百分比
					let rpxHeight = ratio * clientHeight;
					that.windowHeight = rpxHeight;
				}
			})

		},
		onShow: function() {
			this.getUsersAddressList(true)
		},
		methods: {
			itemCleck:function(item){
				if(this.from == 'mallAddress'||this.from == 'mallBillAddress'){
					var prePage = this.$util.prePage()
					prePage.changeAddress(item);
					uni.navigateBack({
						delta: 1
					});
				}
			},
			
			onreachBottomMore: function() {
				if (this.pagination.loadMore) {
					//loadmore为true:底部显示loading,为false,显示"没有更多了"
					this.getUsersAddressList(false);
				}
			},
			//获取地址列表
			getUsersAddressList: function(isRefresh) {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_ADDRESS_GET_USER_ADDRESS_LIST; //刷新页面配置
				var params = {
					"pageNum": isRefresh ? 1 : that.pagination.page,
					"pageSize": that.pagination.limit,
					"userId": app.globalData.userInfo.id,
				};
				network.get(uri, params, 'json').then(res => {
					if (res.code == 200) {
						//课包状态
						var _list = res.data.list;
						var pagination = that.pagination;
						if (isRefresh) {
							that.dataList = _list
						} else {
							if (_list && _list.length > 0) {
								that.dataList = [...that.dataList, ..._list]
							}
						}
						var loadMore = true;
						if (this.pagination.page >= res.data.totalPage) {
							loadMore = false;
						} else {
							loadMore = true;
						}
						that.pagination.loadMore = loadMore
						that.pagination.page = that.pagination.page + 1
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});







			},
			//编辑地址
			editAddress: function(item) {
				uni.navigateTo({
					url: "../editAddress/editAddress?uuid=" + item.uuid + '&isEdit=true',
					success: res => {
						console.log("调用成功")
					},
					fail: () => {
						console.log("调用失败")
					},
					complete: () => {
						console.log("调用完成")
					}
				});
			},
			//新建地址
			addAdress: function(e) {
				uni.navigateTo({
					url: "../editAddress/editAddress",
				});
			},
			//展示删除地址dialog
			showDeleteAddressDialog: function(e) {
				this.show = true
				this.planDeleteId = e.uuid
			},
			//确定
			dialogConfirm(e) {
				this.show = false
				this.deleteAddressById(this.planDeleteId)
			},
			//取消
			dialogCancel() {
				this.show = false
				this.planDeleteId = ""
			},
			//通过id删除数据
			deleteAddressById: function(id) {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_ADDRESS_DELETE_USERS_ADDRESS + "?uuid=" + id;
				var params = {
					// uuid:id 
				};
				network.post(uri, params, 'json').then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})

						this.getUsersAddressList(true)

					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});



			},
			//默认地址
			defaultAddress: function(e) {
				var that = this
				var list = that.dataList
				debugger
				for (var i = 0; i < list.length; i++) {
					if (list[i].uuid == e.uuid) {
						list[i].isDefault = !list[i].isDefault
					} else {
						list[i].isDefault = false
					}
				}

				this.updateUsersAddress(e)


			},
			//编辑地址
			updateUsersAddress: function(e) {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_ADDRESS_UPDATE_USERS_ADDRESS;

				var isDefault = "0"
				if (e.isDefault == true) {
					isDefault = "1"
				} else {
					isDefault = "0"
				}

				var params = {
					uuid: e.uuid,
					userId: app.globalData.userInfo.id,
					isDefault: isDefault
				};
				network.post(uri, params, 'json').then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "更新成功",
							duration: 3000,
							icon: 'none'
						})
						this.getUsersAddressList(true)
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			},







		}
	}
</script>

<style>
	@import "./myAddress.css";
</style>
